package com.cmict.data.infra.controller.admin.message;


import cn.hutool.core.util.ObjectUtil;
import com.cmict.data.framework.common.pojo.CommonResult;
import com.cmict.data.framework.common.pojo.PageResult;
import com.cmict.data.framework.security.core.util.SecurityFrameworkUtils;
import com.cmict.data.infra.controller.admin.message.vo.AnnouncementDTO;
import com.cmict.data.infra.dal.dataobject.message.Announcement;
import com.cmict.data.infra.controller.admin.message.vo.UserAnnouncementQO;
import com.cmict.data.infra.controller.admin.message.vo.MyAnnouncementPageVO;
import com.cmict.data.infra.controller.admin.message.vo.UserAnnouncementPageVO;
import com.cmict.data.infra.service.message.AnnouncementService;
import com.cmict.data.infra.service.message.UserAnnouncementService;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 用户公告表
 *
 * @author hccake 2020-12-25 08:04:53
 */
@RestController
@RequestMapping("/notify/user-announcement")
@Api(tags = " 消息管理 - 我的消息")
public class UserAnnouncementController {

    @Resource
    private UserAnnouncementService userAnnouncementService;
    @Resource
    private AnnouncementService announcementService;

    /**
     * 分页查询（发送详情）
     *
     * @param userAnnouncementQO 用户公告表查询对象
     * @return R 通用返回体
     */
    @GetMapping("/send/page")
    @PreAuthorize("@ss.hasPermission('notify:announcement:read')")
    @Operation(summary = "分页查询（发送详情）", description = "分页查询（发送详情）")
    public CommonResult<PageResult<UserAnnouncementPageVO>> getUserAnnouncementPage(UserAnnouncementQO userAnnouncementQO) {
        return CommonResult.success(userAnnouncementService.queryPage(userAnnouncementQO));
    }

    /**
     * 分页查询（发送详情）
     *
     * @param userAnnouncementQO 用户公告表查询对象
     * @return R 通用返回体
     */
    @GetMapping("/send/templatePage")
    @PreAuthorize("@ss.hasPermission('notify:announcement:read')")
    @Operation(summary = "系统模板分页查询（系统模板发送详情）", description = "分页查询（系统模板发送详情）")
    public CommonResult<PageResult<UserAnnouncementPageVO>> gettemplatePage(UserAnnouncementQO userAnnouncementQO) {
        return CommonResult.success(userAnnouncementService.templatePage(userAnnouncementQO));
    }

    /**
     * 小程序得到 4条 公告和通知
     * @param
     * @return
     */
    @GetMapping("/getAnnouncementsAndNotices")
    @PreAuthorize("@ss.hasPermission('notify:announcement:read')")
    @Operation(summary = "小程序 获取我的公告和通知", description = "小程序 获取我的公告和通知")
    public CommonResult<List<AnnouncementDTO>> getAnnouncementsAndNotices(UserAnnouncementQO userAnnouncementQO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        userAnnouncementQO.setUserId(userId);
        return CommonResult.success(userAnnouncementService.getAnnouncementsAndNotices(userAnnouncementQO));
    }

    /**
     * 发送详情-删除
     *
     * @param id 用户公告表查询对象
     * @return R 通用返回体
     */
    @DeleteMapping("/send/{id}")
    @PreAuthorize("@ss.hasPermission('notify:announcement:del')")
    @Operation(summary = "发送详情-删除", description = "发送详情-删除")
    public CommonResult removeSenDetail(@PathVariable Long id) {
        return CommonResult.success(userAnnouncementService.removeSendDetail(id));
    }

    @PatchMapping("/read")
    @PreAuthorize("@ss.hasPermission('notify:userannouncement:update')")
    @Operation(summary = "用户公告已读上报", description = "用户公告已读上报")
    public CommonResult<Boolean> readAnnouncement(@RequestBody List<Long> announcementId) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        userAnnouncementService.readAnnouncement(userId, announcementId);
        return CommonResult.success(true);
    }

    /**
     * 分页查询（我的消息）
     *
     * @param userAnnouncementQO 用户公告表查询对象
     * @return R 通用返回体
     */
    @PostMapping("/page")
    @PreAuthorize("@ss.hasPermission('notify:userannouncement:read')")
    @Operation(summary = "分页查询（我的消息）", description = "分页查询（我的消息）")
    public CommonResult<PageResult<MyAnnouncementPageVO>> getMyAnnouncementPage(@RequestBody  UserAnnouncementQO userAnnouncementQO) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        userAnnouncementQO.setUserId(userId);
        List<MyAnnouncementPageVO> myAnnouncementPageList = userAnnouncementService.queryMyAnnouncePage(userAnnouncementQO).getList();
        List<MyAnnouncementPageVO> newMyAnnouncementPageList = new ArrayList<>();
        if(ObjectUtil.isNotEmpty(myAnnouncementPageList)){
            for (int i = 0; i <myAnnouncementPageList.size() ; i++) {
                Announcement announcement = announcementService.getAnnouncement(myAnnouncementPageList.get(i).getId());
                myAnnouncementPageList.get(i).setAllFileList(announcement.getAllFileList());
                newMyAnnouncementPageList.add(myAnnouncementPageList.get(i));
            }
        }
        return CommonResult.success(userAnnouncementService.queryMyAnnouncePage(userAnnouncementQO).setList(newMyAnnouncementPageList));
    }

    /**
     * 消息提醒
     *  ordinaryMessage
     *  systemMessage
     * @param userAnnouncementQO 用户公告表查询对象
     * @return R 通用返回体
     */
    @GetMapping("/getRemind")
    @PreAuthorize("@ss.hasPermission('notify:userannouncement:read')")
    @Operation(summary = "消息提醒列表", description = "消息提醒列表")
    public CommonResult<Map<String,Object>> getRemind(UserAnnouncementQO userAnnouncementQO) {

        return CommonResult.success(userAnnouncementService.getRemind(userAnnouncementQO));
    }

    /**
     * 我的消息-删除
     *
     * @param id id
     * @return R 通用返回体
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermission('notify:userannouncement:del')")
    @Operation(summary = "我的消息-删除", description = "我的消息-删除")
    public CommonResult<Boolean> removeById(@PathVariable("id") Long id) {
        return userAnnouncementService.removeId(id) ? CommonResult.success(true)
                : CommonResult.error(500, "通过id删除公告信息失败");
    }


    /**
     * 我的消息-删除
     *
     * @param ids id
     * @return R 通用返回体
     */
    @DeleteMapping("/list")
    @PreAuthorize("@ss.hasPermission('notify:userannouncement:del')")
    @Operation(summary = "我的消息-删除", description = "我的消息-删除")
    public CommonResult<Boolean> removeById(@RequestBody List<Long> ids) {
        return userAnnouncementService.removeByIds(ids) ? CommonResult.success(true)
                : CommonResult.error(500, "通过id删除公告信息失败");
    }
}